<template>
    <div class="header">
        <span class="routeName"></span>
        <el-dropdown trigger="click">
            <span class="userImg">
                <el-avatar shape="square" fit= 'cover' :src="url + imgUrl" />
                <el-icon><CaretBottom /></el-icon>
            </span>
            <template #dropdown>
            <el-dropdown-menu>
                <el-dropdown-item @click="toProfile('/homepage/profile')">个人中心</el-dropdown-item>
                <el-dropdown-item  @click="sendValueToParent">背景设置</el-dropdown-item>
                <el-dropdown-item divided @click="logout()">退出登录</el-dropdown-item>
            </el-dropdown-menu>
            </template>
        </el-dropdown>     
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessageBox, ElMessage } from 'element-plus';
let router = useRouter();
import { defineEmits } from 'vue';

const styleValue = ref(true);

// 定义要触发的自定义事件
const emits = defineEmits(['headerEvent']);

const sendValueToParent = () => {
  styleValue.value = ! styleValue.value;
  // 触发自定义事件 'customEvent'，并传递数据
  emits('headerEvent', styleValue.value);
};

function toProfile(pageUrl) {
    router.push(pageUrl);
}
function logout(){
    ElMessageBox.confirm(
        '你确定要退出吗？',
        '退出登录',
        {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        }
    )
    .then(() => {
        router.push('/login');
        ElMessage({
            type: 'success',
            message: '退出登录',
        })
    })
    .catch(() => {
        ElMessage({
            type: 'info',
            message: '已取消',
        })
    })
}


const url = ref("http://localhost/file/downLoad?fileName=");

const imgUrl = ref(JSON.parse(sessionStorage.getItem('loginInfor')).img);
//console.log(url.value + imgUrl.value);



</script>

<style scoped>
.header{height: 80px; color: gray; font-size: 20px; display: flex; justify-content: space-between; align-items: center; width: 100%;}
.header .routeNmae{padding: 10px; display: block; width: 100px;}
.header .userImg{margin-left: auto;}
</style>